<?php

class Cron extends Controller {

	function Cron()
	{
		parent::Controller();
		
		ini_set("memory_limit","64M");
		set_time_limit(0);
		$this->load->library('simplepie');
		$this->load->helper('debug');
		$this->load->helper('url');
		$this->load->model('mdl_posts','post');
	}
	
	function index()
	{
		$this->db->select("site_id, name, feed_url,(now()-last_update)as diff");
		$this->db->orderby("diff DESC");
		$this->db->limit(1,0);
		$query  = $this->db->get('site');
		$result = $query->row();
		$this->_update($result->feed_url,$result->site_id);
		
		$data['last_update']=date('Y-m-d h:i:s A');
		$this->db->where('site_id',$result->site_id);
		$this->db->update('site',$data);
		
	}
	function _update($link,$id)
	{
		
		$feed = new SimplePie();
		$feed->set_timeout(60);
		$feed->set_feed_url($link);
		$feed->enable_cache(true);
		$feed->init();
		$feed->handle_content_type();
		echo "<h1>UPDATE :".$link."</h1>";		
		$items = $feed->get_items(); 
		foreach ($items as $item)
		{
				$this->post->url		=$item->get_permalink(); 
		 		$this->post->title		=$item->get_title(); 
		 		$this->post->date		= $item->get_date('Y-m-d h:i:s A'); 
		 		$this->post->content	= $item->get_content();
		 		$this->post->site_id	=$id;
		 		
		 		$this->post->save();
	
		 		
		}
	
	}
	echo "<h1 style='color:red'>[DONE]</h1>";
	
	}
}
?>